package com.ywu.data.source.standard.service;

import com.ywu.boot.context.YwuContext;
import com.ywu.boot.context.YwuContextHolder;
import com.ywu.common.entity.PageList;
import com.ywu.common.page.PageService;
import com.ywu.common.result.CommonResult;
import com.ywu.common.utils.TimerIdGenerateUtil;
import com.ywu.data.source.standard.constant.YwuDataSourceConstant;
import com.ywu.data.source.standard.data.YwuDataSourceRepository;
import com.ywu.data.source.standard.dto.YwuDataSourceDto;
import com.ywu.data.source.standard.entity.YwuDataSourceEntity;
import com.ywu.data.source.standard.source.YwuExecSqlCoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName YwuDataSourceService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/10/22 22:56
 * @Version 1.0
 **/
@Service
public class YwuDataSourceService {

    @Autowired
    private YwuDataSourceRepository repository;

    @Autowired
    private YwuExecSqlCoreService execSqlCoreService;

    /**
    * @Author GroundDemo
    * @Description 创建数据源
    * @Date 20:15 2024/10/25
    * @Param [ywuDataSourceDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> createDataSource(YwuDataSourceDto ywuDataSourceDto) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        // 查询当前数据源的编码是否存在
        YwuDataSourceEntity search = new YwuDataSourceEntity();
        search.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        search.setCode(ywuDataSourceDto.getCode());
        YwuDataSourceEntity queryRes = repository.getEntity(search);
        if (Objects.nonNull(queryRes)) {
            return CommonResult.error().setCode(YwuDataSourceConstant.CODE_HAS_EXISTS_CODE)
                    .setErrorCode(YwuDataSourceConstant.CODE_HAS_EXISTS_ERROR_CODE)
                    .setMessage(YwuDataSourceConstant.CODE_HAS_EXISTS_ERROR_MESSAGE);
        }
        // 构建参数
        YwuDataSourceEntity ywuDataSourceEntity = new YwuDataSourceEntity();
        BeanUtils.copyProperties(ywuDataSourceDto, ywuDataSourceEntity);
        ywuDataSourceEntity.setId(TimerIdGenerateUtil.nextId());
        ywuDataSourceEntity.createInfo(currentContext.getYwuContextUser().getUserAccount());
        ywuDataSourceEntity.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        ywuDataSourceEntity.setShare(false);
        // 创建数据库
        execSqlCoreService.createDataSource(ywuDataSourceEntity);
        // 插入数据表
        repository.insertDataSource(ywuDataSourceEntity);
        return CommonResult.success(ywuDataSourceEntity.getId());
    }

    /**
    * @Author GroundDemo
    * @Description 修改数据源
    * @Date 20:17 2024/10/25
    * @Param [ywuDataSourceDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> updateDataSource(YwuDataSourceDto ywuDataSourceDto) {
        // 查询当前数据是否存在
        YwuDataSourceEntity search = new YwuDataSourceEntity();
        search.setId(ywuDataSourceDto.getId());
        search.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        YwuDataSourceEntity queryRes = repository.getEntity(search);
        if (Objects.isNull(queryRes)) {
            return CommonResult.error().setCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_CODE)
                    .setErrorCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_CODE)
                    .setMessage(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_MESSAGE);
        }
        // 构建参数
        YwuDataSourceEntity ywuDataSourceEntity = new YwuDataSourceEntity();
        BeanUtils.copyProperties(ywuDataSourceDto, ywuDataSourceEntity);
        ywuDataSourceEntity.updateInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        // 更新数据库
        ywuDataSourceEntity.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        execSqlCoreService.updateDataSource(ywuDataSourceEntity);
        // 修改数据表
        repository.updateDataSource(ywuDataSourceEntity);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 删除数据源
    * @Date 20:18 2024/10/25
    * @Param [ywuDataSourceDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> deleteDataSource(YwuDataSourceDto ywuDataSourceDto) {
        // 判断当前数据是否存在
        YwuDataSourceEntity search = new YwuDataSourceEntity();
        search.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        search.setId(ywuDataSourceDto.getId());
        YwuDataSourceEntity queryRes = repository.getEntity(search);
        if (Objects.isNull(queryRes)) {
            return CommonResult.error().setCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_CODE)
                    .setErrorCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_CODE)
                    .setMessage(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_MESSAGE);
        }
        // 构建参数
        YwuDataSourceEntity ywuDataSourceEntity = new YwuDataSourceEntity();
        BeanUtils.copyProperties(queryRes, ywuDataSourceEntity);
        // 删除数据库
        ywuDataSourceEntity.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        execSqlCoreService.deleteDataSource(ywuDataSourceEntity);
        // 删除数据表中的数据
        repository.deleteDataSource(ywuDataSourceEntity);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 分页查询数据源信息
    * @Date 20:20 2024/10/25
    * @Param [ywuDataSourceDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryDataSourcePage(YwuDataSourceDto ywuDataSourceDto) {
        ywuDataSourceDto.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        // 查询数量
        int count = repository.queryCount(ywuDataSourceDto);
        // 构建分页参数
        PageService.execPageStartAndEnd(ywuDataSourceDto.getPageInfo());
        // 分页查询
        List<YwuDataSourceEntity> queryList = repository.queryPage(ywuDataSourceDto);
        // 构建pageList对象
        PageList<YwuDataSourceEntity> res = new PageList<>(ywuDataSourceDto.getPageInfo().getPageIndex(),
                ywuDataSourceDto.getPageInfo().getPageSize(), count, queryList);
        return CommonResult.success(res);
    }

    /**
    * @Author GroundDemo
    * @Description 更新是否租户共享
    * @Date 22:41 2024/10/27
    * @Param [ywuDataSourceDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> updateDataSourceShare(YwuDataSourceDto ywuDataSourceDto) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        // 查询ID是否存在
        YwuDataSourceEntity search = new YwuDataSourceEntity();
        search.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        search.setId(ywuDataSourceDto.getId());
        YwuDataSourceEntity queryRes = repository.getEntity(search);
        if (Objects.isNull(queryRes)) {
            return CommonResult.error().setCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_CODE)
                    .setErrorCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_CODE)
                    .setMessage(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_MESSAGE);
        }
        // 修改数据
        YwuDataSourceEntity ywuDataSourceEntity = new YwuDataSourceEntity();
        ywuDataSourceEntity.setId(ywuDataSourceDto.getId());
        ywuDataSourceEntity.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        ywuDataSourceEntity.setShare(ywuDataSourceDto.getShare());
        repository.updateShare(ywuDataSourceEntity);
        return CommonResult.success(null);
    }
}
